package cetcbigdata.da.service.guangdong.at_most_run_once;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.base.DownloadAndUploadMinio;
import cetcbigdata.da.service.guangdong.util.GdUtil;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GuangDongPersonal extends Base {
    private static String baseRegion = "广东省";

    private static String baseModule = "最多跑一次-个人服务";

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "ss", "ss");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        try {
            for(JSONObject r : getRegion()){
                for(JSONObject m : getModule()){
                    this.parseListPage(recordSender, fieldsList, r, m);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private JSONObject parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject moduleItem){
        //获取各个区名与类型名称
        String region = baseRegion + "-" + regionItem.getString("orgName");
        String module = baseModule + "-" + moduleItem.getString("name");
        try{
            // 初始化总页码数
            int pages = 0;
            // 当前页
            int currentPage = 1;
            // 如果当前也不等于总页码数，则一直循环 ，实现翻页
            while (currentPage != pages){
                String listUrl = "https://www.gdzwfw.gov.cn/portal/api/v2/solr/getCommonAuditItem";
                JSONObject parmas = new JSONObject();
                parmas.put("AREA_CODE", regionItem.getString("ORgareaCode"));
                parmas.put("DEPT_CODE", "");
                parmas.put("ISLOCALLEVEL", 0);
                parmas.put("IS_ONLINE", "");
                parmas.put("IS_ZERO_SECNE", "");
                parmas.put("KEY_WORD", "");
                parmas.put("LIMIT_SCENE_SUM", 1);
                parmas.put("PAGENO", currentPage);
                parmas.put("PAGESIZE", 10);
                parmas.put("TASKTAG", 1);
                parmas.put("TASK_TYPE", "");
                parmas.put("TYPE", moduleItem.getString("code"));
                String listResponse =  postReq(listUrl,parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);

                if(responseObj.getJSONObject("STATUS").getString("CODE").equals("00")){
                    JSONArray listTemp = responseObj.getJSONObject("CUSTOM").getJSONArray("AUDIT_ITEMLIST");
                    JSONObject listPageObj = new JSONObject();
                    for(Object o : listTemp){
                        JSONObject item = (JSONObject) o;
                        String taskCode = item.getString("parentCatalogId");
                        if(listPageObj.containsKey(taskCode)){
                            listPageObj.getJSONArray(taskCode).add(item);
                        }else {
                            listPageObj.put(taskCode, new JSONArray());
                            listPageObj.getJSONArray(taskCode).add(item);
                        }
                    }

                    // 每页每项遍历
                    for(String key : listPageObj.keySet() ){
                        JSONObject contentParent =new JSONObject();
                        String title = null;
                        String detailUrl = null;
                        String contentResponse = null;
                        for(Object o : listPageObj.getJSONArray(key)){
                            JSONObject item = (JSONObject) o;
                            detailUrl = String.format("https://www.gdzwfw.gov.cn/portal/v2/guide/%s",
                                    item.getString("TASK_CODE"));
                            title = item.getString("TASK_NAME");
                        }
                        contentParent.put("名称", title);
                        contentParent.put("网址", detailUrl);
                        processItem(
                                processResult(listUrl, listResponse,detailUrl,contentResponse, contentParent,
                                        region,
                                        module
                                ),
                                recordSender,
                                fieldsList
                        );
                    }
                    if(listTemp.size() > 0){
                        currentPage += 1;
                    }else {
                        break;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JSONObject();
    }

    //获取市名和各市编码
    private static JSONArray getCityList(){
        String city = " [\n" +
                "{\"name\":\"广州市\",\"region\":\"440100\"},    \n" +
                "{\"name\":\"深圳市\",\"region\":\"440300\"}, \n" +
                "{\"name\":\"珠海市\",\"region\":\"440400\"},\n" +
                "{\"name\":\"汕头市\",\"region\":\"440500\"},\n" +
                "{\"name\":\"佛山市\",\"region\":\"440600\"},\n" +
                "{\"name\":\"韶关市\",\"region\":\"440200\"},\n" +
                "{\"name\":\"河源市\",\"region\":\"441600\"},\n" +
                "{\"name\":\"梅州市\",\"region\":\"441400\"},\n" +
                "{\"name\":\"惠州市\",\"region\":\"441300\"},\n" +
                "{\"name\":\"汕尾市\",\"region\":\"441500\"},\n" +
                "{\"name\":\"东莞市\",\"region\":\"441900\"},\n" +
                "{\"name\":\"中山市\",\"region\":\"442000\"},\n" +
                "{\"name\":\"江门市\",\"region\":\"440700\"},\n" +
                "{\"name\":\"阳江市\",\"region\":\"441700\"},\n" +
                "{\"name\":\"湛江市\",\"region\":\"440800\"},\n" +
                "{\"name\":\"茂名市\",\"region\":\"440900\"},\n" +
                "{\"name\":\"肇庆市\",\"region\":\"441200\"},\n" +
                "{\"name\":\"揭阳市\",\"region\":\"445200\"},\n" +
                "{\"name\":\"清远市\",\"region\":\"441800\"},\n" +
                "{\"name\":\"潮州市\",\"region\":\"445100\"},\n" +
                "{\"name\":\"云浮市\",\"region\":\"445300\"},\n" +
                "{\"name\":\"横琴粤澳深度合作区\",\"region\":\"440405\"}\n" +
                " ]";
        return JSONArray.parseArray(city);
    }

    public static  List<JSONObject> getRegion(){
        //获取各市编码
        JSONArray cityList = getCityList();
        List<JSONObject> out = new ArrayList<>();
        for (Object o : cityList){
            JSONObject oo = (JSONObject) o;
            String code = oo.getString("region");

            String url = "https://www.gdzwfw.gov.cn/portal/api/v2/node/gdbsNav/current";
            JSONObject header = new JSONObject();
            header.put("Referer", "https://www.gdzwfw.gov.cn/portal/v2/personal/hot?region=" + code);
            header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
            JSONObject params = new JSONObject();
            params.put("regCode", code);

            String response = postFormReq(url, header, params);
            JSONObject data = JSONObject.parseObject(response);
            JSONArray dataList = data.getJSONArray("hall");
            for (Object d : dataList){
                JSONObject dd = (JSONObject) d;
                JSONObject outItem = new JSONObject();
                String name = oo.getString("name") + "-" + dd.getString("ORGSNAME");
                outItem.put("orgName", name);
                outItem.put("ORgareaCode", dd.getString("ORGAREACODE"));
                out.add(outItem);
            }
        }
        return out;
    }

    private static List<JSONObject> getModule(){
        //获取类型名称和编码
        String data = "{\"THEMES\":[{\"CODE\":\"001001001\",\"NAME\":\"生育收养\",\"ORDERNUM\":1},{\"CODE\":\"001001002\",\"NAME\":\"教育科研\",\"ORDERNUM\":2},{\"CODE\":\"001001003\",\"NAME\":\"文化体育\",\"ORDERNUM\":3},{\"CODE\":\"001001004\",\"NAME\":\"入伍服役\",\"ORDERNUM\":4},{\"CODE\":\"001001005\",\"NAME\":\"就业创业\",\"ORDERNUM\":5},{\"CODE\":\"001001006\",\"NAME\":\"婚姻登记\",\"ORDERNUM\":6},{\"CODE\":\"001001007\",\"NAME\":\"纳税\",\"ORDERNUM\":7},{\"CODE\":\"001001008\",\"NAME\":\"住房保障\",\"ORDERNUM\":8},{\"CODE\":\"001001009\",\"NAME\":\"职业资格\",\"ORDERNUM\":9},{\"CODE\":\"001001010\",\"NAME\":\"社会保障（社会保险、社会救助）\",\"ORDERNUM\":10},{\"CODE\":\"001001011\",\"NAME\":\"医疗卫生\",\"ORDERNUM\":11},{\"CODE\":\"001001012\",\"NAME\":\"户籍办理\",\"ORDERNUM\":12},{\"CODE\":\"001001015\",\"NAME\":\"交通出行\",\"ORDERNUM\":13},{\"CODE\":\"001001017\",\"NAME\":\"出境入境\",\"ORDERNUM\":14},{\"CODE\":\"001001018\",\"NAME\":\"司法公证\",\"ORDERNUM\":15},{\"CODE\":\"001001020\",\"NAME\":\"死亡殡葬\",\"ORDERNUM\":16},{\"CODE\":\"001001021\",\"NAME\":\"其他\",\"ORDERNUM\":17},{\"CODE\":\"001001059\",\"NAME\":\"民族宗教\",\"ORDERNUM\":18},{\"CODE\":\"001001060\",\"NAME\":\"社会救助\",\"ORDERNUM\":19},{\"CODE\":\"001001061\",\"NAME\":\"消费维权\",\"ORDERNUM\":20},{\"CODE\":\"001001062\",\"NAME\":\"知识产权\",\"ORDERNUM\":21},{\"CODE\":\"001001063\",\"NAME\":\"行政缴费\",\"ORDERNUM\":22},{\"CODE\":\"001001064\",\"NAME\":\"优待抚恤\",\"ORDERNUM\":23},{\"CODE\":\"001001065\",\"NAME\":\"规划建设\",\"ORDERNUM\":24},{\"CODE\":\"001001066\",\"NAME\":\"证件办理\",\"ORDERNUM\":25},{\"CODE\":\"001001067\",\"NAME\":\"旅游观光\",\"ORDERNUM\":26},{\"CODE\":\"001001068\",\"NAME\":\"公共安全\",\"ORDERNUM\":27},{\"CODE\":\"001001069\",\"NAME\":\"公用事业\",\"ORDERNUM\":28},{\"CODE\":\"001001070\",\"NAME\":\"城市综合执法\",\"ORDERNUM\":29},{\"CODE\":\"001001071\",\"NAME\":\"养老服务\",\"ORDERNUM\":30},{\"CODE\":\"001001072\",\"NAME\":\"涉农补贴\",\"ORDERNUM\":31},{\"CODE\":\"001001073\",\"NAME\":\"义务教育\",\"ORDERNUM\":32},{\"CODE\":\"001001074\",\"NAME\":\"准营准办\",\"ORDERNUM\":33},{\"CODE\":\"001001075\",\"NAME\":\"设立变更\",\"ORDERNUM\":34},{\"CODE\":\"001001076\",\"NAME\":\"环保绿化\",\"ORDERNUM\":35},{\"CODE\":\"001001077\",\"NAME\":\"抵押质押\",\"ORDERNUM\":36},{\"CODE\":\"001001078\",\"NAME\":\"离职退休\",\"ORDERNUM\":37},{\"CODE\":\"001001079\",\"NAME\":\"地方特色分类\",\"ORDERNUM\":38}]}";
        JSONArray validData = JSONObject.parseObject(data).getJSONArray("THEMES");
        List<JSONObject> out = new ArrayList<>();
        for(Object o : validData){
            JSONObject oItem = (JSONObject) o;
            JSONObject outItem = new JSONObject();
            outItem.put("code", oItem.getString("CODE"));
            outItem.put("name", oItem.getString("NAME"));
            out.add(outItem);
        }
        return out;
    }

    public static void main(String[] args) {
        GuangDongPersonal spider = new GuangDongPersonal();
        spider.getData(null, new ArrayList<>());
    }
}
